﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Configuration;
using System.Web.Security;
using System.Collections;

public partial class ChangePassword : System.Web.UI.Page
{
    string NewPasswd = "";
    string OldPasswd = "";
    int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["gid"] == null)
        {
            Response.Redirect("./Login.aspx");
        }
        ErrorReport.Visible = false;
    }
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        string OldPasswd = OldPassword.Text;
        DBaccess db = new DBaccess();
        SqlDataReader read = db.GetReader("select password from mr_member where mid='" + Session["mid"].ToString() + "'");
        read.Read();
        if (OldPasswd == read.GetValue(0).ToString())
        {
            ErrorReport.Text = "";
            if ((RegConfirm(NewPassword.Text) == 1) && (RegConfirm(PasswordConfirm.Text) == 1))
            {
                if (NewPassword.Text == PasswordConfirm.Text)
                {
                    NewPasswd = NewPassword.Text;
                    if (NewPasswd.Length > 20)
                    {
                        ErrorReport.Visible = true;
                        ErrorReport.BorderWidth = 1;
                        ErrorReport.BackColor = Color.FromArgb(255, 174, 201);
                        ErrorReport.ForeColor = Color.Red;
                        ErrorReport.Text = "您输入的新密码过长，请检查后正确输入！";
                        NewPassword.Text = "";
                        PasswordConfirm.Text = "";
                        NewPasswd = "";
                    }
                    
                    try
                    {
                       
                            id = Int16.Parse(Session["mid"].ToString());
                            NewPasswd = FormsAuthentication.HashPasswordForStoringInConfigFile(NewPasswd, "MD5");
                            DBaccess db1 = new DBaccess();
                            int read1 = db.ExecSql("update mr_member set password='" + NewPasswd + "' where mid='" + id + "'");
                            //sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["testLottery"].ConnectionString);
                            // sqlcon.Open();
                            // cmd = "update Member set password='" + NewPasswd + "' where uid='" + id + "'";
                            Session["password"] = NewPasswd;
                        
                        //sqlcmd = new SqlCommand(cmd, sqlcon);
                        ErrorReport.BorderWidth = 0;
                        ErrorReport.BackColor = Color.Red;
                        ErrorReport.ForeColor = Color.Black;
                        ErrorReport.Text = "修改成功！";
                        ErrorReport.Visible = true;
                        //sqlcmd.ExecuteNonQuery();
                    }
                    catch (SqlException sqle)
                    {
                        ;
                    }
                    finally
                    {
                       // sqlcon.Close();
                    }
                }
                else
                {
                    ErrorReport.Visible = true;
                    ErrorReport.ForeColor = Color.Red;
                    ErrorReport.BorderWidth = 1;
                    ErrorReport.BackColor = Color.FromArgb(255, 174, 201);
                    ErrorReport.Text = "两次密码不一致，请重新输入！";
                    NewPassword.Text = "";
                    PasswordConfirm.Text = "";
                    OldPassword.Text = "";
                }
            }
            else
            {
                ErrorReport.Visible = true;
                ErrorReport.ForeColor = Color.Red;
                ErrorReport.BorderWidth = 1;
                ErrorReport.BackColor = Color.FromArgb(255, 174, 201);
                ErrorReport.Text = "新密码格式不对，请重新输入！";
                NewPassword.Text = "";
                PasswordConfirm.Text = "";
                OldPassword.Text = "";
            }
        }
        else
        {
            ErrorReport.Visible = true;
            ErrorReport.ForeColor = Color.Red;
            ErrorReport.BorderWidth = 1;
            ErrorReport.BackColor = Color.FromArgb(255, 174, 201);
            ErrorReport.Text = "旧密码错误，请核实！";
            NewPassword.Text = "";
            PasswordConfirm.Text = "";
            OldPassword.Text = "";
        }
    }

    private int RegConfirm(string str)
    {
        String TextRegex = "^[A-Za-z0-9]+$";
        if ((Regex.IsMatch(str, TextRegex)))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if ((int)Session["gid"] == 1)
        {
            Response.Redirect("../Teacher/Teacher.aspx");
        }
        else if ((int)Session["gid"] == 2)
        {
            Response.Redirect("../Student/Student.aspx");
        }
        else
        {
            Response.Redirect("../Login.aspx");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["search"] = TextBox7.Text;
        Response.Redirect("./Search.aspx");
    }
}